<?
ini_set("display_errors",1);
include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/portals/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');

init_session();

if (empty($_SESSION['user_id'])) {
	include($_SERVER['DOCUMENT_ROOT'].'/portals/login.inc.php');
	exit;
}

include($_SERVER['DOCUMENT_ROOT'].'/portals/header.inc.php');



$portal_id = $id = intval(@$_GET['id']);



if ($id) {
	$where = "`readable` = 'Y'"; 
	if (!empty($_SESSION['user_id'])) {
		$where = "($where OR user_id = ".dbQuote($_SESSION['user_id']).")";
	}
	$current = getRow("SELECT * FROM portal WHERE portal_id = $id AND $where");

        if ($current['status_id'] != 1) {
                die("unable to view this Portal");
        }

	
	print "<h3>Suggest addition of new images</h3>";
} else {
	die("huh?");
}



if ($id ) {
	print "<p>For Portal: <b><a href=\"http://".he($current['domain']).".portal.geographs.org/\">".he($current['title'])."</a></b>. ";

	if (!empty($_SESSION['user_id'])) {
		print "<a href=\"portal.php?id=$id\">Return to listing</a></p>";
	}
	
	if (!empty($current['description'])) {
		print "<p><small>".he($current['description'])."</small></p>";
	}
}

if (!empty($_POST) && !empty($_POST['submit'])) {
	
	$filter_id = getOne("SELECT filter_id FROM filter WHERE portal_id = $portal_id AND user_id = {$_SESSION['user_id']} AND type='any' AND profile = 'ids' AND status_id = 1 LIMIT 1");

	$ids = '';

	if (!empty($_POST['previds'])) 
		$ids = trim(preg_replace('/[^\d]+/',' ',$_POST['previds']));
	
	if (!empty($_POST['ids'])) 
		$ids = $ids.' '.trim(preg_replace('/[^\d]+/',' ',$_POST['ids']));

	$updates = array();
						
	$updates['value'] = trim($ids);

	if (!empty($updates)) {

		if ($filter_id) {
			if ($current_filter['status_id'] == 0 && empty($updates['status_id']))
				$updates['status_id'] = 1;

			$updates['last_run'] = 0;
			$sql= updates_to_update('filter',$updates,'filter_id',$filter_id);
		} else {
			$updates['portal_id'] = $portal_id;
			$updates['user_id'] = $_SESSION['user_id'];
			$updates['type'] = 'any';
			$updates['profile'] = 'ids';
			$updates['title'] = 'Suggestions by '.$_SESSION['realname'];
			$updates['created'] = 'NOW()';
			$sql= updates_to_insert('filter',$updates);
		}

		queryExecute($sql);

		if (!$filter_id) {
			$filter_id = mysql_insert_id();
		}


		//log the updates
		$updates = array();
		$updates['value'] = trim($ids);
	
		foreach ($updates as $key => $value) {
			if (!is_null($value)) {
				$u = array();
				$u['table'] = 'filter';
				$u['table_id'] = $filter_id;
				$u['name'] = $key;
				$u['value'] = $value;
				$u['user_id'] = $_SESSION['user_id'];
				$sql= updates_to_insert('update_log',$u);
				queryExecute($sql);
			}
		}
		
		print "<h4>Updates saved. Note: It can take at least a few minutes before images start showing in the portal</h4>";
	}
	

}


?>

<form action="suggest.php?id=<? echo $id; ?>" style="background-color:#eeeeee;padding:10px" method="post">

<? if (!empty($current['suggest_note'])) {
	print "<p>Specific note from the portal curator:<br/><b>". MakeLinks(he($current['suggest_note']))."</b></p>";
} ?>

<p>I suggest adding the following images from Geograph Britain &amp; Ireland:<br/>
<textarea name="ids" rows="5" cols="60"></textarea></p>
<small>Seperate ids by spaces, commas, new lines etc, can also just copy/paste photo page url, ids in brackets work too. Don't worry about duplicates.<br/>
Example of a valid submission: <tt>1234,23456 345 http://www.geograph.org.uk/photo/45678 [[[56789]]] [[12345678]]</tt></small><br/><br/>

<input type="submit" name="submit" value="Send"><br/><br/>



<? 

$ids = getOne("SELECT value FROM filter WHERE portal_id = $portal_id AND user_id = {$_SESSION['user_id']} AND type='any' AND profile = 'ids' LIMIT 1");

if (!empty($ids)) {
	?>
	<hr/>
	<p><b>Your previous suggestions</b>: (remove a id from here to withdraw the suggestion)<br/>
	<textarea name="previds" rows="3" cols="70" style="font-size:0.9em"><? echo he($ids); ?></textarea></p>
	<?	
}

?>

</form>

<br/>
<p><b>You can use following search to find images to suggest, or to check what images already in the portal.</b></p>

<form method="get" action="http://www.nearby.org.uk/geograph/search-service.php" target="inner">
	Keywords: <input type="text" name="q" size=30"><input type="submit" value="Search"/>
	
	In: <input type=radio name="scope" value="all" checked onclick="this.form.action='http://www.nearby.org.uk/geograph/search-service.php';"> All images /
	 <input type=radio name="scope" value="portal" onclick="this.form.action='http://<? echo he($current['domain']); ?>.portal.geographs.org/search.php';"> in this portal
	<input type="hidden" name="inner" value="1">
	
</form>

<iframe src="about:blank" width="100%" height="500" id="inner" name="inner"></iframe>

<?

include($_SERVER['DOCUMENT_ROOT'].'/portals/footer.inc.php');
